//
//  DQRSXDetailTableViewController.m
//  uplife
//
//  Created by ZhangCheng on 2018/12/27.
//  Copyright © 2018年 ZhangCheng. All rights reserved.
//

#import "DQRSXDetailTableViewController.h"
#import "FileListViewController.h"
@interface DQRSXDetailTableViewController ()
@property(nonatomic,weak)IBOutlet UIButton *tongZuJinBtn;
@property(nonatomic,weak)IBOutlet UIView *textFieldSuperView;
@property(nonatomic,weak)IBOutlet UIButton *tongZuJinBtn2;
@property(nonatomic,weak)IBOutlet UIView *textFieldSuperView2;

@property(nonatomic,weak)IBOutlet UILabel *contentLabel;
@property(nonatomic,weak)IBOutlet UILabel *dateLabel;
@property(nonatomic,weak)IBOutlet UILabel *titleLabel;
@property(nonatomic,weak)IBOutlet UILabel *fangJianHaoLabel;
@property(nonatomic,weak)IBOutlet UILabel *typeLabel;
@property(nonatomic,weak)IBOutlet UITextField *inputTF;

@property(nonatomic,weak)IBOutlet UITextField *disabelInputTF;
@property(nonatomic,weak)IBOutlet UIView *disabelSuperView;
@property(nonatomic,weak)IBOutlet UIView *disableLeaseSubView;
@property(nonatomic,weak)IBOutlet UIView *disableOtherView;
@property(nonatomic,weak)IBOutlet UIButton *disableOtherAgreeBtn;
@property(nonatomic,weak)IBOutlet UIView *enabelSuperView;
@property(nonatomic,weak)IBOutlet UIView *enableLeaseSubView;
@property(nonatomic,weak)IBOutlet UIView *enableOtherSubView;

@property(nonatomic,strong)NSDictionary *dic;
@end

@implementation DQRSXDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tongZuJinBtn.layer.masksToBounds = YES;
    [self.tongZuJinBtn.layer setBorderColor:[UIColor colorWithRed:148.0f/255.0f green:110.0f/255.0f blue:41.0f/255.0f alpha:1.0f].CGColor];
    [self.textFieldSuperView.layer setBorderColor:[UIColor colorWithRed:148.0f/255.0f green:110.0f/255.0f blue:41.0f/255.0f alpha:1.0f].CGColor];
    self.textFieldSuperView.layer.masksToBounds = YES;
    
    self.tongZuJinBtn2.layer.masksToBounds = YES;
    [self.tongZuJinBtn2.layer setBorderColor:[UIColor colorWithRed:148.0f/255.0f green:110.0f/255.0f blue:41.0f/255.0f alpha:1.0f].CGColor];
    [self.textFieldSuperView2.layer setBorderColor:[UIColor colorWithRed:148.0f/255.0f green:110.0f/255.0f blue:41.0f/255.0f alpha:1.0f].CGColor];
    self.textFieldSuperView2.layer.masksToBounds = YES;
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self sendRequestForLoadData];
}

-(void)sendRequestForLoadData{
    NSString *urlString = [NSString stringWithFormat:@"%@/message/get-one.do?id=%@",JSXF_HOST,self.dataId];
    
    [SVProgressHUD show];
    
    [ZCWebServiceHelper requestWithMethod:GET andUrlStr:urlString andParameters:nil andFinished:^(id response, NSURLSessionDataTask *dataTask) {
        [SVProgressHUD dismiss];
        NSDictionary *dic = response;
        if ([dic[@"data"] isKindOfClass:[NSDictionary class]]) {
            self.dic = dic[@"data"];
            [self resetView];
            [self.tableView reloadData];
        }
    }];
}

-(IBAction)submitAction{
    NSString *inputString = [self.inputTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (inputString.length==0) {
        [SVProgressHUD showErrorWithStatus:@"请输入价格"];
        return;
    }
    
    NSString *urlString = [NSString stringWithFormat:@"%@/message/affirm.do?id=%@&nowPrice=%@",JSXF_HOST,self.dataId,inputString];
    
    [SVProgressHUD show];
    
    [ZCWebServiceHelper requestWithMethod:GET andUrlStr:urlString andParameters:nil andFinished:^(id response, NSURLSessionDataTask *dataTask) {
        [SVProgressHUD showSuccessWithStatus:@"操作成功"];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

-(IBAction)submitForAgree:(UIButton*)sender{
    NSString *urlString = [NSString stringWithFormat:@"%@/message/affirm.do?id=%@&isAgree=%@",JSXF_HOST,self.dataId,sender.tag==998?@"1":@"0"];
    
    [SVProgressHUD show];
    
    [ZCWebServiceHelper requestWithMethod:GET andUrlStr:urlString andParameters:nil andFinished:^(id response, NSURLSessionDataTask *dataTask) {
        [SVProgressHUD showSuccessWithStatus:@"操作成功"];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

-(IBAction)sameAction{
    NSString *urlString = [NSString stringWithFormat:@"%@/message/affirm.do?id=%@&isSamePrice=1",JSXF_HOST,self.dataId];
    
    [SVProgressHUD show];
    
    [ZCWebServiceHelper requestWithMethod:GET andUrlStr:urlString andParameters:nil andFinished:^(id response, NSURLSessionDataTask *dataTask) {
        [SVProgressHUD showSuccessWithStatus:@"操作成功"];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

-(void)resetView{
    self.dateLabel.text = [NSString stringWithFormat:@"%@",self.dic[@"sendTime"]];
    self.contentLabel.text = [NSString stringWithFormat:@"%@",self.dic[@"content"]];
    self.disabelInputTF.text = [NSString stringWithFormat:@"%@",self.dic[@"nowPrice"]];
    self.textFieldSuperView2.hidden=self.disabelInputTF.text.length==0;
    BOOL isAgree = [NSString stringWithFormat:@"%@",self.dic[@"isAgree"]].integerValue==1;
    BOOL isSamePrice = [NSString stringWithFormat:@"%@",self.dic[@"isSamePrice"]].integerValue==1;
    self.tongZuJinBtn2.hidden = !isSamePrice;
    [self.disableOtherAgreeBtn setTitle:(isAgree?@"已同意":@"已驳回") forState:UIControlStateDisabled];
    NSString *typeCode = self.dic[@"typeCode"];
    self.typeLabel.text = [[ZCTool sharedInstance]typeNameForMessageTypeCode:typeCode];
    BOOL isLease =![typeCode isEqualToString:@"LEASE"];
//    self.tongZuJinBtn.hidden = !isLease;
    self.disableOtherView.hidden = !isLease;
    self.disableLeaseSubView.hidden = isLease;
    self.enableLeaseSubView.hidden = isLease;
    self.enableOtherSubView.hidden = !isLease;
    
    NSString *titleString = [NSString stringWithFormat:@"%@",self.dic[@"title"]];
    NSString *fangJianHaoString = [NSString stringWithFormat:@"#%@",self.dic[@"roomNum"]];
    NSString *isShowRoomNum = [NSString stringWithFormat:@"%@",self.dic[@"isShowRoomNum"]];
    BOOL isShowRoom = [isShowRoomNum isEqualToString:@"1"];
    if (isShowRoom==NO) {
        fangJianHaoString = @"";
    }
    self.fangJianHaoLabel.hidden = !isShowRoom;
    self.fangJianHaoLabel.text = fangJianHaoString;
    CGFloat width = [UIScreen mainScreen].bounds.size.width - 31.0f;
        /**
         5.根据text的font和字符串自动算出size（重点）
         200:你希望的最大宽度
         MAXFLOAT:最大高度为最大浮点数
         **/
        CGSize size1 = [titleString boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                                        options:NSStringDrawingUsesLineFragmentOrigin
                                                     attributes:@{NSFontAttributeName:self.titleLabel.font}
                                                        context:nil].size;
        CGFloat height1 = size1.height;
        CGSize size2 = [[NSString stringWithFormat:@"%@ %@",titleString,fangJianHaoString] boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                                                                                               options:NSStringDrawingUsesLineFragmentOrigin
                                                                                                            attributes:@{NSFontAttributeName:self.titleLabel.font}
                                                                                                               context:nil].size;
        CGFloat height2 = size2.height;
        
        if (height1==height2) {
            self.titleLabel.text = titleString;
        }else{
            self.titleLabel.text = [NSString stringWithFormat:@"%@\n",titleString];
        }
    for (NSInteger i=11; i<17; i++) {
        UILabel *label = [self.tableView viewWithTag:i];
        NSNumber *fileCount = self.dic[@"files"][i-11][@"fileCount"];
        label.text = [NSString stringWithFormat:@"%@",fileCount];
        UITableViewCell *cell = [self.tableView viewWithTag:(i+10)];
        cell.accessoryType = fileCount.integerValue==0?UITableViewCellAccessoryNone:UITableViewCellAccessoryDisclosureIndicator;
    }
    
    NSNumber *isSureNum = self.dic[@"isSure"];
    BOOL isSure = isSureNum.integerValue!=0;
//    BOOL isSure = NO;
    self.disabelSuperView.hidden = !isSure;
    self.enabelSuperView.hidden = isSure;
    
    BOOL isOther =[typeCode isEqualToString:@"OTHER"];
    if (isOther) {
        self.disabelSuperView.hidden = YES;
        self.enabelSuperView.hidden = YES;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        return UITableViewAutomaticDimension;

    }else{
        return 55.0f;
    }
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        return UITableViewAutomaticDimension;
        
    }else{
        return 55.0f;
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row!=0) {
        NSNumber *fileCount = self.dic[@"files"][indexPath.row-1][@"fileCount"];
        if (fileCount.integerValue!=0) {
            [self performSegueWithIdentifier:@"toFilesListViewController" sender:self.dic[@"files"][indexPath.row-1]];
        }
    }
}
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"toFilesListViewController"]) {
        FileListViewController *vc = segue.destinationViewController;
        vc.dataDic = sender;
    }
}
@end
